package com.mcc.web.controller.system;

import java.util.*;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mcc.common.config.ProjectConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.mcc.common.constant.Constants;
import com.mcc.common.core.domain.AjaxResult;
import com.mcc.common.core.domain.entity.SysMenu;
import com.mcc.common.core.domain.entity.SysUser;
import com.mcc.common.core.domain.model.LoginBody;
import com.mcc.common.core.domain.model.LoginUser;
import com.mcc.common.utils.ServletUtils;
import com.mcc.framework.web.service.SysLoginService;
import com.mcc.framework.web.service.SysPermissionService;
import com.mcc.framework.web.service.TokenService;
import com.mcc.system.service.ISysMenuService;

/**
 * 登录验证
 *
 * @author gxy
 */
@Api(tags = "登陆")
@ApiSort(11)
@RestController
public class SysLoginController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ProjectConfig projectConfig;

    @ApiOperation("登录方法")
    @ApiOperationSupport(order = 1)
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode());
        Map<String, String> data = new HashMap<>();
        data.put(Constants.TOKEN, token);
        return AjaxResult.success(data);
    }

    @ApiOperation("获取用户信息")
    @ApiOperationSupport(order = 2)
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("user", user);
        data.put("roles", roles);
        data.put("permissions", permissions);
        return AjaxResult.success(data);
    }

    @ApiOperation("获取路由信息")
    @ApiOperationSupport(order = 3)
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus, null));
    }

    @ApiOperation("获取验证码配置")
    @ApiOperationSupport(order = 4)
    @GetMapping("getCaptchaConfig")
    public AjaxResult getCaptchaConfig() {
        return AjaxResult.success(projectConfig.isCaptchaEnabled());
    }
}
